<?php
header ( "Content-type:text/html;charset=utf-8" );
// 关闭notice错误
/*
 * error_reporting ( 0 );
 */
define ( 'D_BUG', '1' );
$_SGLOBAL = array ();
$_SC = array ();
$_SC ['dbhost'] = '127.0.0.1'; // 服务器地址
$_SC ['dbuser'] = 'root'; // 用户
$_SC ['dbpw'] = 'root'; // 密码
$_SC ['dbcharset'] = 'utf8'; // 字符集
$_SC ['pconnect'] = 0; // 是否持续连接
$_SC ['dbname'] = 'iEnjoy'; // 数据库
$_SC ['charset'] = 'utf-8'; // 页面字符集
                            
// 时间
$mtime = explode ( ' ', microtime () );
$_SGLOBAL ['timestamp'] = $mtime [1];
$_SGLOBAL ['supe_starttime'] = $_SGLOBAL ['timestamp'] + $mtime [0];
// 链接数据库
dbconnect ();

$radionum = $reqnum = $trwupnum = $pbnum = $qbupdatenum = 0;
$radionum1 = $reqnum1 = $trwupnum1 = $pbnum1 = $qbupdatenum1 = 0;

// 蜻蜓数据中，tbl_radio表中 已存在的，以电台之家数据为准，修改
$urqt = searchRadio ();

$rcount = $count = $scount =0;

while ( $uvalue = $_SGLOBAL ['db']->fetch_array ( $urqt ) ) {
	
	
	
	
	/*
	 * 常州新闻台
	 * http://www.radio366.com/asx/136.asx
	 * $urlContent = get_url_content ( 'http://www.radio366.com/bf.asp?bh=136' );
	*/
	
	/*
	 * 山东经济广播
	 * #rtmp://video.iqilu.com/multicast_push2/sdradio09
	 * $urlContent = get_url_content ( 'http://www.radio366.com/bf.asp?bh=58' );
	 */
	
	/*
	 * 重庆都市广播
	 * #rtmp://live5.cqnews.net:1935/live/&file=GBFLV5&type=video&autostart=true&controlbar=bottom&caption=false&fullscreen=true
	 * $urlContent = get_url_content ( 'http://www.radio366.com/bf.asp?bh=55' );
	 */
	
	/*
	 * 东广新闻台
	 * #http://live-cdn1.smgbb.tv/channels/bbtv/audio_dgxw/flv:fm/live&file=livestream&type=video&autostart=true&controlbar=bottom&caption=false&	
	 * $urlContent = get_url_content ( 'http://www.radio366.com/bf.asp?bh=32' );
     */
	
    /*
     * 中央台中国之声
     * #mms://cdnmms.cnr.cn/cnr001?MzE5MTg0IzEjIzI5NjgwOQ==
     * $urlContent = get_url_content ( 'http://www.radio366.com/bf.asp?bh=1' );
	*/
	
	
	$urlContent = get_url_content($uvalue['play_url']);
	
	 $content = iconv ( 'GB2312', 'UTF-8//IGNORE', $urlContent );
	
	
	// 电台网络链接
	$play_url = '';
	
	if (preg_match ( '/<script>diantai\((.*?)\);<\/script>/is', $content, $array )) {
		
		$hid = $array [1];
		if ($hid) {
			$hurl = 'http://www.radio366.com/iframe/' . $hid . '.html';
			
			
			$hcontent = get_url_content ( $hurl );
			
			
			if (preg_match ( '/<embed.*flashvars="(.*?)".*>/is', $hcontent, $array )) {
				
				$play_url = str_replace ( 'streamer=', '', $array [1] );
			
			} elseif (preg_match ( '/<iframe.*src="(.*?)".*>/is', $hcontent, $array )) {
				$cnurl = ($array [1]);
				$ccontent = get_url_content ( $cnurl );
				
				
				if (preg_match ( '/show_media\("(.*?)","/is', $ccontent, $array )) {
					$play_url = $array [1];
				}elseif(preg_match ( '/flashvars.url="(.*?)"/is', $ccontent, $array )){
					$play_url = $array[1];
				
				}
			
			}
		
		}
	} elseif (preg_match ( '/<embed.*src="(.*?)".*>/is', $content, $array )) {
		$play_url = 'http://www.radio366.com/' . $array [1];
	
	}
	
	if(!$play_url){
		
		$count++;
	}
	$rcount++;
	#$uvalue['play_url'];
	/* echo "<br>";
	echo $play_url;echo "<br>"; */
	if ($play_url) {
		
		$insertsqlarr ['play_url'] = $play_url;
		$wherearr ['id'] = $uvalue ['id'];
		updatetable ( 'tbl_radio', $insertsqlarr, $wherearr );
		$scount++;
	}
}
echo "需要补齐的地址数量为：".$rcount;
echo "没有采集到地址的数量为：".$count;
echo "修改的网络电台地址的数量为：".$scount;
// 数据库连接
function dbconnect() {
	global $_SGLOBAL, $_SC;
	// include_once ('class_mysql.php');
	$_SGLOBAL ['db'] = new dbstuff ();
	$_SGLOBAL ['db']->charset = $_SC ['dbcharset'];
	$_SGLOBAL ['db']->connect ( $_SC ['dbhost'], $_SC ['dbuser'], $_SC ['dbpw'], $_SC ['dbname'], $_SC ['pconnect'] );
}

function get_url_content($url) {
	// user_agent
	$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
	// 伪造header
	$header = array ('Accept-Language: zh-cn', 'Connection: Keep-Alive', 'Cache-Control: no-cache' );
	$ch = curl_init ();
	$timeout = 5;
	curl_setopt ( $ch, CURLOPT_REFERER, $url );
	curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
	curl_setopt ( $ch, CURLOPT_USERAGENT, $useragent );
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_VERBOSE, 0 );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
	$file_contents = curl_exec ( $ch );
	curl_close ( $ch );
	return $file_contents;
}

function dump($arr) {
	echo "<pre>";
	var_dump ( $arr );
	echo "<pre>";
}

function inserttable($tablename, $insertsqlarr, $returnid = 0, $replace = false, $silent = 0) {
	global $_SGLOBAL, $_SC;
	$tablename = $tablename;
	
	$insertkeysql = $insertvaluesql = $comma = '';
	foreach ( $insertsqlarr as $insert_key => $insert_value ) {
		$insertkeysql .= $comma . '`' . $insert_key . '`';
		$insertvaluesql .= $comma . '\'' . $insert_value . '\'';
		$comma = ', ';
	}
	$method = $replace ? 'REPLACE' : 'INSERT';
	$_SGLOBAL ['db']->query ( $method . ' INTO ' . $tablename . ' (' . $insertkeysql . ') VALUES (' . $insertvaluesql . ')', $silent ? 'SILENT' : '' );
	if ($returnid && ! $replace) {
		return $_SGLOBAL ['db']->insert_id ();
	}
}

function updatetable($tablename, $insertsqlarr, $wherearr) {
	global $_SGLOBAL, $_SC;
	$tablename = $tablename;
	
	$insertkeysql = $insertvaluesql = $comma = '';
	foreach ( $insertsqlarr as $insert_key => $insert_value ) {
		$insertkeysql .= $comma . '`' . $insert_key . '` = ' . '\'' . $insert_value . '\'';
		$comma = ', ';
	}
	$wheresql = " where ";
	foreach ( $wherearr as $w_key => $w_value ) {
		if ($w_key == 0) {
			$wheresql = $wheresql . $w_key . "=" . $w_value;
		} else {
			$wheresql = $wheresql . " and " . $w_key . "=" . $w_value;
		}
	}
	
	if ($_SGLOBAL ['db']->query ( 'update ' . $tablename . ' set ' . $insertkeysql . $wheresql )) {
		return 1;
	} else {
		return 0;
	}

}

function searchRadio() {
	global $_SGLOBAL, $_SC;
	
	$sql = "select id,play_url from tbl_radio where play_url like 'http://www.radio366.com/bf.asp?bh=%'";
	
	$query_offica1 = $_SGLOBAL ['db']->query ( $sql );
	
	return $query_offica1;
	// eturn $_SGLOBAL['db']->fetch_array($query_offica1);
}

function getRadioQt($isnull = 0) {
	global $_SGLOBAL, $_SC;
	$andstr = '';
	if ($isnull == 1) {
		$andstr = 'where tr.name="" or tr.name is null';
	} else {
		$andstr = "where  tr.name!='' and tqc.name!=''";
	}
	$sql = 'select 
					tqc.channelid,tqc.rid,tqc.name,tqc.description,tqc.type,tqc.frequency,tr.id,tr.name as rname,tr.channel,tr.description as rdescription,tr.play_url,source 
					from tbl_qt_channels tqc 
					left join tbl_radio tr 
					on tr.name=tqc.name ' . $andstr; // " limit 10";
	
	$query_offica1 = $_SGLOBAL ['db']->query ( $sql );
	
	return $query_offica1;
	// eturn $_SGLOBAL['db']->fetch_array($query_offica1);
}

function getRadioProgram($channelid = 0) {
	global $_SGLOBAL;
	
	$sql = 'select * from tbl_qt_channel_programs where channelid=' . $channelid;
	
	$query_offica1 = $_SGLOBAL ['db']->query ( $sql );
	
	return $query_offica1;
	// eturn $_SGLOBAL['db']->fetch_array($query_offica1);
}

function getRadioFreq($channelid = 0) {
	global $_SGLOBAL;
	
	$sql = 'select * from tbl_radio_freq where radio_id=' . $channelid;
	
	$query_offica1 = $_SGLOBAL ['db']->query ( $sql );
	
	return $query_offica1;
	// eturn $_SGLOBAL['db']->fetch_array($query_offica1);
}

function getPlayBill($channelid = 0, $starttime) {
	global $_SGLOBAL;
	
	$sql = 'select * from tbl_playbill where radio_id=' . $channelid . " and start_time='" . $starttime . "'";
	
	$query_offica1 = $_SGLOBAL ['db']->query ( $sql );
	
	return $query_offica1;
	// eturn $_SGLOBAL['db']->fetch_array($query_offica1);
}

class dbstuff {
	var $querynum = 0;
	var $link;
	var $charset;
	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE) {
		if ($pconnect) {
			if (! $this->link = @mysql_pconnect ( $dbhost, $dbuser, $dbpw )) {
				$halt && $this->halt ( 'Can not connect to MySQL server' );
			}
		} else {
			if (! $this->link = @mysql_connect ( $dbhost, $dbuser, $dbpw, 1 )) {
				$halt && $this->halt ( 'Can not connect to MySQL server' );
			}
		}
		
		if ($this->version () > '4.1') {
			if ($this->charset) {
				@mysql_query ( "SET character_set_connection=$this->charset, character_set_results=$this->charset, character_set_client=binary", $this->link );
			}
			if ($this->version () > '5.0.1') {
				@mysql_query ( "SET sql_mode=''", $this->link );
			}
		}
		if ($dbname) {
			@mysql_select_db ( $dbname, $this->link );
		}
	}
	
	function select_db($dbname) {
		return mysql_select_db ( $dbname, $this->link );
	}
	
	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array ( $query, $result_type );
	}
	
	function query($sql, $type = '') {
		if (D_BUG) {
			global $_SGLOBAL;
			$sqlstarttime = $sqlendttime = 0;
			$mtime = explode ( ' ', microtime () );
			$sqlstarttime = number_format ( ($mtime [1] + $mtime [0] - $_SGLOBAL ['supe_starttime']), 6 ) * 1000;
		}
		$func = $type == 'UNBUFFERED' && @function_exists ( 'mysql_unbuffered_query' ) ? 'mysql_unbuffered_query' : 'mysql_query';
		if (! ($query = $func ( $sql, $this->link )) && $type != 'SILENT') {
			$this->halt ( 'MySQL Query Error', $sql );
		}
		if (D_BUG) {
			$mtime = explode ( ' ', microtime () );
			$sqlendttime = number_format ( ($mtime [1] + $mtime [0] - $_SGLOBAL ['supe_starttime']), 6 ) * 1000;
			$sqltime = round ( ($sqlendttime - $sqlstarttime), 3 );
			
			$explain = array ();
			$info = mysql_info ();
			if ($query && preg_match ( "/^(select )/i", $sql )) {
				$explain = mysql_fetch_assoc ( mysql_query ( 'EXPLAIN ' . $sql, $this->link ) );
			}
			$_SGLOBAL ['debug_query'] [] = array ('sql' => $sql, 'time' => $sqltime, 'info' => $info, 'explain' => $explain );
		}
		$this->querynum ++;
		return $query;
	}
	
	function affected_rows() {
		return mysql_affected_rows ( $this->link );
	}
	
	function error() {
		return (($this->link) ? mysql_error ( $this->link ) : mysql_error ());
	}
	
	function errno() {
		return intval ( ($this->link) ? mysql_errno ( $this->link ) : mysql_errno () );
	}
	
	function result($query, $row) {
		$query = @mysql_result ( $query, $row );
		return $query;
	}
	
	function num_rows($query) {
		$query = mysql_num_rows ( $query );
		return $query;
	}
	
	function num_fields($query) {
		return mysql_num_fields ( $query );
	}
	
	function free_result($query) {
		return mysql_free_result ( $query );
	}
	
	function insert_id() {
		return ($id = mysql_insert_id ( $this->link )) >= 0 ? $id : $this->result ( $this->query ( "SELECT last_insert_id()" ), 0 );
	}
	
	function fetch_row($query) {
		$query = mysql_fetch_row ( $query );
		return $query;
	}
	
	function fetch_fields($query) {
		return mysql_fetch_field ( $query );
	}
	
	function version() {
		return mysql_get_server_info ( $this->link );
	}
	
	function close() {
		return mysql_close ( $this->link );
	}
	
	function halt($message = '', $sql = '') {
		$dberror = $this->error ();
		$dberrno = $this->errno ();
		$help_link = "http://faq.comsenz.com/?type=mysql&dberrno=" . rawurlencode ( $dberrno ) . "&dberror=" . rawurlencode ( $dberror );
		echo "<div style=\"position:absolute;font-size:11px;font-family:verdana,arial;background:#EBEBEB;padding:0.5em;\">
		<b>MySQL Error</b><br>
		<b>Message</b>: $message<br>
		<b>SQL</b>: $sql<br>
		<b>Error</b>: $dberror<br>
		<b>Errno.</b>: $dberrno<br>
		<a href=\"$help_link\" target=\"_blank\">Click here to seek help.</a>
		</div>";
		exit ();
	}
}

